

#include "BeastHttpServerRequestHandler.h"
#include <iostream>
#include "../WebModule/WebModule.h"

namespace http = boost::beast::http;


bool HttpApiHttpServerRequestHandler::handle_request(const beast::http::request<beast::http::string_body>& req, beast::http::response<beast::http::string_body>& res)
{
    //TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_DEBUG, "enter UKeyHttpServerRequestHandler::handle_request");

    std::string target;
#if _MSC_VER
    target = req.target().to_string();
#else
    target = req.target();
#endif
    std::string body = req.body();

    std::replace(target.begin(), target.end(), '/', '_');

    std::string res_content;

    MapServerFunc(target, body, res_content);

    http::response<http::string_body> api_res{
        std::piecewise_construct,
        std::make_tuple(std::move(body)),
        std::make_tuple(http::status::ok, req.version()) };

    api_res.set(http::field::server, "you guess!");
    api_res.body() = res_content;
    api_res.prepare_payload();
    api_res.keep_alive(req.keep_alive());
    api_res.set(http::field::access_control_allow_origin, "*");


    res = std::move(api_res);

    //TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_DEBUG, "leave UKeyHttpServerRequestHandler::handle_request");

    return true;
}


